﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WPFWindow = System.Windows.Window;
using EnvDTE;
using VSIXUtilities.GUI;
using VSIXUtilities.Helpers.Extensions;
using VSIXUtilities.DataObjects;
using VSIXUtilities.Helpers;
using EnvDTE80;

namespace CodeDocumentationSynchronizer.CodDocSync.GUI
{
    /// <summary>
    /// Interaction logic for WindowSyncCurrentMember.xaml
    /// </summary>
    public partial class WindowSyncCurrentMember : WPFWindow
    {
        int intCurrentCheckedHierarchyItems;
        CodeElementHelper CodeElementHelperInstance = new CodeElementHelper();
        CodeElement[] CurrentCodeElements;
        public CodeElement SelectedElement
        {
            get;
            private set;
        }
        public WindowSyncCurrentMember(CodeElement[] CurrentCodeElements)
        {
            InitializeComponent();
            this.CurrentCodeElements = CurrentCodeElements;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (CurrentCodeElements.Length > 0)
            {
                ToggleButtonTreeViewItem<RadioButton> ToggleButtonTreeViewItemCodeElement = CreateToggleButtonTreeViewItem(CurrentCodeElements[0], true);
                TreeViewCodeElements.Items.Add(ToggleButtonTreeViewItemCodeElement);
                for (int intCodeElementIndex = 1; intCodeElementIndex < CurrentCodeElements.Length; intCodeElementIndex++)
                {
                    ToggleButtonTreeViewItem<RadioButton> ToggleButtonTreeViewItemChild = CreateToggleButtonTreeViewItem(CurrentCodeElements[intCodeElementIndex], intCodeElementIndex == CurrentCodeElements.Length - 2);
                    ToggleButtonTreeViewItemCodeElement.Items.Add(ToggleButtonTreeViewItemChild);
                    ToggleButtonTreeViewItemCodeElement = ToggleButtonTreeViewItemChild;
                }
            }
        }

        private void ToggleButtonTreeViewItem_Checked(object sender, RoutedEventArgs e)
        {
            SelectedElement = (CodeElement)((ToggleButtonTreeViewItem<RadioButton>)sender).Tag;
            ListViewHierarchy.Items.Clear();
            if (SelectedElement is CodeNamespace)
            {
                IVsHierarchyWrapper IVsHierarchyWrapperCurrentProject = CodDocSyncPackage.SelectionEvents.CurrentProject;
                if (IVsHierarchyWrapperCurrentProject != null)
                {
                    foreach (IVsHierarchyWrapper IVsHierarchyWrapperNamespaceContainer in CodeElementHelperInstance.SearchForNamespace(IVsHierarchyWrapperCurrentProject, SelectedElement))
                    {
                        AddListViewItem(IVsHierarchyWrapperNamespaceContainer);
                    }
                }
            }
            else if (SelectedElement.CanBePartial())
            {
                foreach (CodeElement CodeElementPart in SelectedElement.GetParts())
                {
                    AddListViewItem(CodeElementPart.ProjectItem.ToIVsHierarchyWrapper());
                }
            }
            else
            {
                AddListViewItem(SelectedElement.ProjectItem.ToIVsHierarchyWrapper());
            }
            intCurrentCheckedHierarchyItems = ListViewHierarchy.Items.Count;
            ManageOKAvailability();
        }

        private void CheckBoxHierarchyItem_Checked(object sender, RoutedEventArgs e)
        {
            intCurrentCheckedHierarchyItems++;
            ManageOKAvailability();
        }

        private void CheckBoxHierarchyItem_Uncheced(object sender, RoutedEventArgs e)
        {
            intCurrentCheckedHierarchyItems--;
            ManageOKAvailability();
        }

        private ToggleButtonTreeViewItem<RadioButton> CreateToggleButtonTreeViewItem(CodeElement CodeElementInstance, bool Checked)
        {
            ImageSource ImageSourceInstance = CodeElementInstance.GetImageSource();
            ToggleButtonTreeViewItem<RadioButton> ToggleButtonTreeViewItemResult = new ToggleButtonTreeViewItem<RadioButton>(CodeElementInstance.Name, false, ImageSourceInstance.GetImage(), ImageSourceInstance.GetImage());
            ToggleButtonTreeViewItemResult.ToggleButtonInstance.GroupName = "ABC";
            ToggleButtonTreeViewItemResult.Tag = CodeElementInstance;
            ToggleButtonTreeViewItemResult.IsExpanded = true;
            ToggleButtonTreeViewItemResult.Checked += new RoutedEventHandler(ToggleButtonTreeViewItem_Checked);
            ToggleButtonTreeViewItemResult.IsChecked = Checked;
            return ToggleButtonTreeViewItemResult;
        }

        private void AddListViewItem(IVsHierarchyWrapper IVsHierarchyWrapperInstance)
        {
            //ListViewHierarchy.Items.Add(IVsHierarchyWrapperInstance);
            ListViewHierarchy.Items.Add(new
            {
                Text = IVsHierarchyWrapperInstance.LogicalPath,
                CollapsedStateImage = IVsHierarchyWrapperInstance.Bitmap.ToWPFImageSource(),
                ExpandedStateImage = IVsHierarchyWrapperInstance.OpenFolderBitmap.ToWPFImageSource(),
                Tag = IVsHierarchyWrapperInstance
            });
            //ToggleButtonTreeViewItem<CheckBox> parent = new ToggleButtonTreeViewItem<CheckBox>(IVsHierarchyWrapperInstance.LogicalPath, true, IVsHierarchyWrapperInstance.Bitmap, IVsHierarchyWrapperInstance.OpenFolderBitmap)
            //{
            //    IsEnabled = Enabled,
            //    Tag = IVsHierarchyWrapperInstance
            //};
            //parent.Items.Add(new ToggleButtonTreeViewItem<CheckBox>("child", true));
            //ListViewHierarchy.Items.Add(parent);
        }

        //private void AddListViewItem(IVsHierarchyWrapper IVsHierarchyWrapperInstance, bool Enabled)
        //{
        //    ListViewHierarchy.Items.Add(new ToggleButtonTreeViewItem<CheckBox>(IVsHierarchyWrapperInstance.LogicalPath, true, IVsHierarchyWrapperInstance.Bitmap, IVsHierarchyWrapperInstance.OpenFolderBitmap)
        //    {
        //        IsEnabled = Enabled,
        //        Tag = IVsHierarchyWrapperInstance
        //    });
        //}

        private void ManageOKAvailability()
        {
            ButtonOK.IsEnabled = intCurrentCheckedHierarchyItems > 0;
        }

        private void ButtonOK_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        //public IVsHierarchyWrapper[] CheckedHierarchyItems
        //{
        //    get
        //    {
        //        return ListViewHierarchy.Items.w
        //    }
        //}
    }
}
